package dk.lector.domain;

import java.util.concurrent.ExecutionException;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.net.Uri.Builder;
import android.os.AsyncTask;
import android.util.Log;
import dk.lector.R;
import dk.lector.datasource.DocDBRepository;
import dk.lector.exception.TSMobileException;
import dk.lector.providers.DOC_METADATAContentProvider;
import dk.lector.providers.SEARCHContentProvider;
import dk.lector.providers.SEARCH_RESULTContentProvider;
import dk.lector.remote.TSMobileService;
import dk.lector.utils.TSProgressDialog;

public class DocFacade {

	public static Uri saveSearch(final Context appContext,
			final String documentID, final String documentName, final String username, final String password) {	
		
		AsyncTask<Void, Void, Uri> searchForDocumentsTask = new AsyncTask<Void, Void, Uri>() {

			@Override
			protected Uri doInBackground(Void... arg0) {
				
				final String id = documentID.equals("") ? "default" : documentID;
				final String name = documentName.equals("") ? "default"
						: documentName;		

				return DocDBRepository.createSearch(appContext, id, name);		
			}
		};
		
		try {
			return searchForDocumentsTask.execute().get();
		} catch (ExecutionException e) {
			throw new TSMobileException(e, DocFacade.class, "search()", "");
		} catch (InterruptedException e) {
			throw new TSMobileException(e, DocFacade.class, "search()", "");
		}
	}

	public static Cursor searchDocumentsByUri(final Context appContext, final Uri uri, final String username, final String password) {
		AsyncTask<Void, Void, Cursor> async = new AsyncTask<Void, Void, Cursor>(){
			@Override
			protected Cursor doInBackground(Void... params) {
				final Cursor c = DocDBRepository.getSearchByUri(appContext, uri);
				final int searchID;
				if (c != null && c.moveToFirst()){
					searchID = c.getInt(c.getColumnIndex(SEARCHContentProvider._ID));
					new Thread(new Runnable() {
						public void run() {
							TSMobileService.searchDocuments(appContext, 
									searchID, 
									Integer.toString(c.getInt(c.getColumnIndex(SEARCHContentProvider.SEARCH_DOC_ID))), 
									c.getString(c.getColumnIndex(SEARCHContentProvider.SEARCH_NAME)), 
									username, 
									password);
							c.close();	
						}
					}).start();
				} else {
					c.close();
					throw new TSMobileException(new NullPointerException(), DocFacade.class, "SearchDocumentsByUri", "No search found in database with given uri " + uri.toString());
				}
						
				
				return DocDBRepository.getDocumentsBySearchID(appContext, searchID);
			};
		};
		try {
			return async.execute().get();
		} catch (InterruptedException e) {
			throw new TSMobileException(e, DocFacade.class, "searchDocumentsByUri()", "");
		} catch (ExecutionException e) {
			throw new TSMobileException(e, DocFacade.class, "searchDocumentsByUri()", "");
		}
	}
	
	public static Cursor getFavorites(final Context appContext, final String username, final String password) {
		AsyncTask<Void, Void, Cursor> async = new AsyncTask<Void, Void, Cursor>(){
			@Override
			protected Cursor doInBackground(Void... params) {
	
					new Thread(new Runnable() {
						public void run() {
							TSMobileService.getFavorites(appContext, username, password);
							
						}
					}).start();
				
				return DocDBRepository.getFavorites(appContext);
			};
		};
		try {
			return async.execute().get();
		} catch (InterruptedException e) {
			throw new TSMobileException(e, DocFacade.class, "searchDocumentsByUri()", "");
		} catch (ExecutionException e) {
			throw new TSMobileException(e, DocFacade.class, "searchDocumentsByUri()", "");
		}
	}	
	
	public void getDocumentMetaDataByID(Context appContext, int documentID, String username, String password) {
		AsyncTask<Void, Void, Void> async = new AsyncTask<Void, Void, Void>(){
			@Override
			protected Void doInBackground(Void... params) {
				
				return null;
			};
		};
		async.execute();
	}
	public static Cursor getLocalDocuments(final Context appContext) {
		AsyncTask<Void, Void, Cursor> async = new AsyncTask<Void, Void, Cursor>(){
			@Override
			protected Cursor doInBackground(Void... params) {
	
				return DocDBRepository.getLocalDocuments(appContext);
			};
		};
		try {
			return async.execute().get();
		} catch (InterruptedException e) {
			throw new TSMobileException(e, DocFacade.class, "getLocalDocuments()", "");
		} catch (ExecutionException e) {
			throw new TSMobileException(e, DocFacade.class, "getLocalDocuments", "");
		}
	}	


}
